package com.project.common.domain;

import cn.dev33.satoken.stp.StpUtil;
import com.project.common.domain.po.BasePO;
import javax.persistence.PrePersist;
import javax.persistence.PreUpdate;

/**
 * jpa用户信息
 */
public class DataScopeAware  {
    @PrePersist
    private void prePersist(BasePO entity) {
        try {
            if(StpUtil.isLogin()){
                StpUtil.checkLogin();
                if(StpUtil.getExtra("roleKeys")!=null){
                    entity.setRoleKeys(StpUtil.getExtra("roleKeys").toString());
                }
                if(StpUtil.getExtra("orgKey")!=null){
                    entity.setOrgKey(StpUtil.getExtra("orgKey").toString());
                }
            }
        }catch (Exception e){

        }
    }

    @PreUpdate
    private void preUpdate(BasePO entity) {
        try {
            if(StpUtil.isLogin()){
                StpUtil.checkLogin();
                if(StpUtil.getExtra("roleKeys")!=null){
                    entity.setRoleKeys(StpUtil.getExtra("roleKeys").toString());
                }
                if(StpUtil.getExtra("orgKey")!=null){
                    entity.setOrgKey(StpUtil.getExtra("orgKey").toString());
                }
            }
        }catch (Exception e){

        }
    }
}
